package com.atguigu.study.config;

import com.atguigu.study.service.ChatPersistenceAssistant;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Marco
 * @Description
 * @date 2025/8/21 15:05
 * @email 3293336923@qq.com
 */
@Configuration
public class LLMConfig {

    @Autowired
    private   RedisChatMemoryStore  redisChatMemoryStore;
    @Bean
    public ChatModel chatModel() {
        return OpenAiChatModel.builder()
                .apiKey(System.getenv("ALIQW"))
                .modelName("qwen-plus")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .build();
    }

    @Bean
    public ChatPersistenceAssistant chatPersistenceAssistant(ChatModel chatModel){


//        聊天记忆
       ChatMemoryProvider   chatMemoryProvider =
               memoryId-> MessageWindowChatMemory.builder()
//                       存储 memoryId 的 最大消息数量为 1000 ， 并且将消息存储在 redisChatMemoryStore 中
                       .id(memoryId)
                       .maxMessages(1000)
                       .chatMemoryStore(redisChatMemoryStore)
                       .build();

       return  AiServices.builder(ChatPersistenceAssistant.class)
                .chatModel(chatModel)
               .chatMemoryProvider(chatMemoryProvider)
                .build();


//              return  AiServices.builder(ChatPersistenceAssistant.class)
//                .chatModel(chatModel)
//                .build();
//        这两行代码的含义是一样的
//     return   AiServices.create(ChatPersistenceAssistant.class , chatModel);


    }
}
